home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 46
/
Amiga Format CD46 (1999-10-20)(Future Publishing)(GB)[!][issue 1999-12].iso
/
-in_the_mag-
/
reader_requests
/
scilab
/
demos
/
intro
/
dem01.dem
Wrap
Text File
|
1999-09-16
|
5KB
|
191 lines
mode(7)
// SCILAB OBJECTS
// 1. SCALARS
a=1 //constant
1==1 //boolean
'string' //character string
z=poly(0,'z') // polynomial with variable 'z' and with one root at zero
p=1+3*z+4.5*z^2 //polynomial
r=z/p //rational
// 2. MATRICES
a=[a+1 2 3
0 0 atan(1)
5 9 -1] //constant matrix
b=[%t,%f] //boolean matrix
mc=['this','is';
'a' ,'matrix'] //matrix of strings
mp=[p,1-z;
1,z*p] //polynomial matrix
mp=[p 1-z]
mp=[mp;1 1+z*p] //matrix polynomial
f=mp/poly([1+%i 1-%i 1],'z') //rational matrix
// 3. LISTS
l=list(a,-(1:5), mp,['this','is';'a','list']) //list
b=[1 0;0 1;0 0];c=[1 -1 0];d=0*c*b;x0=[0;0;0];
sl=syslin('c',a,b,c,d,x0) //Linear system in state-space representation.
slt=ss2tf(sl) // Transfer matrix
// OPERATIONS
v=1:5;v*v' //constant matrix
mp'*mp+eye //polynomial matrix
mp1=mp(1,1)+4.5*%i //complex
fi=c*(z*eye-a)^(-1)*b; //transfer function evaluation
f(:,1)*fi //rationals
m=[mp -mp; mp' mp+eye] //usual Matlab syntax for polynomials
[fi, fi(:,1)] // ... or rationals
f=syslin('c',f);
num=f(2);den=f(3); //operation on transfer matrix
// SOME NUMERICAL PRIMITIVES
inv(a) //Inverse
inv(mp) //Inverse
inv(sl*sl') //Product of two linear systems and inverse
w=ss2tf(ans) //Transfer function representation
inv(ss2tf(sl)*ss2tf(sl')) //Product of two transfer functions and inverse
clean(w-ans)
n=contr(a,b) //Controllability
k=ppol(a,b,[-1-%i -1+%i -1]) //Pole placement
poly(a-b*k,'z')-poly([-1-%i -1+%i -1],'z') //Check...
s=sin(0:0.1:5*%pi);
ss=fft(s(1:128),-1); //FFT
xbasc();
plot2d3("enn",1,abs(ss)'); //simple plot
x=lyap(a,diag([1 2 3]),'cont') //Lyapunov equation
// ON LINE DEFINITION OF MACRO
deff('[x]=fact(n)','if n=0 then x=1,else x=n*fact(n-1),end')
10+fact(5)
// OPTIMIZATION
deff('[f,g,ind]=rosenbro(x,ind)', 'a=x(2)-x(1)^2 , b=1-x(2) ,...
f=100.*a^2 + b^2 , g(1)=-400.*x(1)*a , g(2)=200.*a -2.*b ');
comp(rosenbro);[f,x,g]=optim(rosenbro,[2;2],'qn')
// SIMULATION
a=rand(3,3)
e=exp(a)
deff('[ydot]=f(t,y)','ydot=a*y');comp(f)
e(:,1)-ode([1;0;0],0,1,f)
// SYSTEM DEFINITION
s=poly(0,'s')
h=[1/s,1/(s+1);1/s/(s+1),1/(s+2)/(s+2)]
w=tf2ss(h);
ss2tf(w)
h1=clean(ans)
// EXAMPLE: SECOND ORDER SYSTEM ANALYSIS
sl=syslin('c',1/(s*s+0.2*s+1))
instants=0:0.05:20;
// step response:
y=csim('step',instants,sl);
xbasc();plot2d(instants',y')
// Delayed step response
deff('[in]=u(t)','if t<3 then in=0;else in=1;end');
comp(u);
y1=csim(u,instants,sl);plot2d(instants',y1');
// Impulse response;
yi=csim('imp',instants,sl);xbasc();plot2d(instants',yi');
yi1=csim('step',instants,s*sl);plot2d(instants',yi1');
// Discretization
dt=0.05;
sld=dscr(tf2ss(sl),0.05);
// Step response
u=ones(instants);
yyy=flts(u,sld);
xbasc();plot(instants,yyy)
// Impulse response
u=0*ones(instants);u(1)=1/dt;
yy=flts(u,sld);
xbasc();plot(instants,yy)
// system interconnexion
w1=[w,w];
clean(ss2tf(w1))
w2=[w;w];
clean(ss2tf(w2))
// change of variable
z=poly(0,'z');
horner(h,(1-z)/(1+z)) //bilinear transform
// PRIMITIVES
H=[1. 1. 1. 0.;
2. - 1. 0. 1;
1. 0. 1. 1.;
0. 1. 2. - 1];
ww=spec(H)
// STABLE SUBSPACES
[X,d]=schur(H,'cont');
X'*H*X
[X,d]=schur(H,'disc');
X'*H*X
//Selection of user-defined eigenvalues (# 3 and 4 here);
deff('[flg]=sel(x)','flg=0,ev=x(2)/x(3),if abs(ev-ww(3))<0.0001|abs(ev-ww(4))<0.00001 then flg=1,end')
[X,d]=schur(H,sel)
X'*H*X
// With matrix pencil
[X,d]=gschur(H,eye(H),sel)
X'*H*X
// block diagonalization
[ab,x,bs]=bdiag(H);
inv(x)*H*x
// Matrix pencils
E=rand(3,2)*rand(2,3);
A=rand(3,2)*rand(2,3);
s=poly(0,'s');
w=det(s*E-A) //determinant
[al,be]=gspec(A,E);
al./(be+%eps*ones(be))
roots(w)
[Ns,d]=coffg(s*E-A); //inverse of polynomial matrix;
clean(Ns/d*(s*E-A))
[Q,M,i1]=pencan(E,A); // Canonical form;
M*E*Q
M*A*Q
// PAUSE-RESUME
write(%io(2),'pause command...');
write(%io(2),'TO CONTINUE...');
write(%io(2),'ENTER ''resume (or return) or click on resume!!''');
//pause;
// CALLING EXTERNAL ROUTINE
foo=[' subroutine foo(a,b,c)';
' c=a+b';
' end' ];
unix_s('rm -f foo.f')
write('foo.f',foo);
unix_s('make foo.o') //Compiling...(needs fortran compiler)
//..................WARNING.......................
//NEXT COMMAND LINE WILL DO THE LINK OF THE ROUTINE foo WITH SCILAB
//THIS COMMAND MAY FAIL FOR SystemV COMPILED VERSIONS OF SCILAB
//BECAUSE LINK NEEDS THE LIBRARIES (SEE THE HELP OF "LINK" COMMAND)
link('foo.o','foo') //Linking to Scilab
//5+7 by fortran
fort('foo',5,1,'r',7,2,'r','out',[1,1],3,'r')